<?php
defined('IN_TS') or die('Access Denied.');  

class pay extends tsApp{
	
	//构造函数
	public function __construct($db){
		parent::__construct($db);
	}
	
	
	/*
	 * 获取支付帐号
	 */
	public function getUserPay($userid){
		$isUser = $this->findCount('pay',array(
			'userid'=>$userid,
		));
		
		if($isUser==0){
			//创建个支付帐号
			$this->create('pay',array(
				'userid'=>$userid,
				'total'=>0,
				'over'=>0,
			));
		}
	
		$strPay = $this->find('pay',array(
			'userid'=>$userid,
		));
		
		return $strPay;
	}
	

	/**
	 * 消费更新支付帐号
	 * @status 0收入 1消费
	 * @statusinfo 收入和消费说明
	 */
	public function updatePay($userid,$price,$status=0,$giftid){
	
		$strPay = $this->getUserPay($userid);
		
		if($status){
			$arr = array(
				'over'=>$strPay['over']-$price,
			);
			
		}else{
			$arr = array(
				'over'=>$strPay['over']+$price,
				'total'=>$strPay['total']+$price,
			);
		}
		
		$this->update('pay',array(
			'userid'=>$userid,
		),$arr);
		
		$this->updateLog($userid,$price,$status,$giftid);
	
	}
	
	//增加收入和消费记录
	public function updateLog($userid,$price,$status=0,$giftid){
		$this->create('pay_log',array(
			'userid'=>$userid,
			'price'=>$price,
			'status'=>$status,
			'giftid'=>$giftid,
			'addtime'=>time(),
		));
	}
	
	
}
